﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.Linq;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace ChatData
{
  
    public enum Status
    {
        Avaliable,
        Busy,
        Invisible,
        Unavaliable,
    }
    public interface IGetType
    {
        bool IsFile();
    }
        public class Utils
        {
            // From here;http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/417c81dc-52f8-4869-8738-bbd478056935/
            public static string GetLocalIPAddress()
            {
                // Get the IP Broadcast address and convert it to string.
                System.Net.IPAddress[] ipAddressString = System.Net.Dns.GetHostAddresses(Environment.MachineName);
                return ipAddressString[1].ToString();
            }
            public static IPAddress GetIPAddress()
            {
                // Get the IP Broadcast address and convert it to string.
                System.Net.IPAddress[] ipAddressString = System.Net.Dns.GetHostAddresses(Environment.MachineName);
                return ipAddressString[1];
            }

            // kept to maintian baclkwards compatibility
            public static string EncodePasswordToBase64(string password)
            {
                return CreateSaltedPassword("thesaltshaker", password, 10000);
            }
            //from here: http://stackoverflow.com/questions/312088/what-is-the-easiest-way-to-create-and-compare-a-salted-password-in-net
            public static string CreateSaltedPassword(string salt, string password, int iter)
            {
                SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
                byte[] byteValue = System.Text.Encoding.UTF8.GetBytes(salt + password);
                do
                {
                    byteValue = SHA1.ComputeHash(byteValue);
                } while (--iter > 0);
                SHA1.Clear();
                return Convert.ToBase64String(byteValue);
            }

            private static T GetNewObject<T>()
            {
                try
                {
                    return (T)typeof(T).GetConstructor(new Type[] { }).Invoke(new object[] { });
                }
                catch
                {
                    return default(T);
                }
            }
            private static byte[] ReadStream(Stream s)
        {
            byte[] buffer = new byte[4095];
            int numBytesToRead = (int)s.Length;
            int numBytesRead = 0;
            while (numBytesToRead > 0)
            {
                // Read may return anything from 0 to numBytesToRead.
                int n = s.Read(buffer, numBytesRead, numBytesToRead);
                // The end of the file is reached.
                if (n == 0)
                    break;
                numBytesRead += n;
                numBytesToRead -= n;
            }
            return buffer;
        }
            public static IPAddress getIPAddress(String IP)
            {
                return  IPAddress.Parse(IP);
            }
            public static string GetString(Stream thestream)
        {
            int n = thestream.ReadByte();
            byte[] bytes = new byte[n];
            thestream.Read(bytes, 0, n);
            return Encoding.ASCII.GetString(bytes);
        }
    }

        }      

